﻿<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Admin.Master" Inherits="System.Web.Mvc.ViewPage<dynamic>" %>

<%@ Import Namespace="B2B.Models" %>

<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">

<h2>AdminManageProduct</h2>

</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="HEAD" runat="server">
</asp:Content>

<asp:Content ID="Content3" ContentPlaceHolderID="main_content" runat="server">
    <script type="text/javascript"> 
        
        function loadDanhMuc(maLinhVuc)
        {
            if (maLinhVuc != 0) {
                $.getJSON("/Home/DanhSachDanhMuc?id=" + maLinhVuc,
                 function (data) {
                     document.getElementById("divDanhMuc").innerText = "";
                     var s = '';
                     s += "<select id='MaDanhMuc'>";
                     for (i = 0 ; i < data.length; i++) {
                         s += "<option value='" + data[i].MaDanhMuc + "'>" + data[i].TenDanhMuc + "</option>";
                     }
                     s += "</select>";
                     $("#divDanhMuc").html(s);
                 });
            }
            else
            {
                document.getElementById("checkCategory").innerText = "Please select a item in category";
            }
        }

        function CapNhat(masp)
        {
            var tensp= document.getElementById("NameProduct").value;
            var g = document.getElementById("Price").value;
            var sltt = document.getElementById("MinimumOrderQuantity").value;
            var kncu = document.getElementById("SupplyCapacity").value;
            var ltt = document.getElementById("PaymentType").value;
            
            if(document.getElementById("selProductCategory").value==0)
            {
                var mdm = document.getElementById("hidMaDanhMuc").value;
            }
            else
            {
                var mdm = document.getElementById("MaDanhMuc").value;
            }

            var mqg = document.getElementById("Country").value;
            var mtv = document.getElementById("Member").value;
            var mdvt = document.getElementById("Unit").value;
            var mtt = document.getElementById("Status").value;
            var ct = document.getElementById("Detail").value;
            var pathimg = document.getElementById("ImagePath").value;
            var capnhathinh = 1;
            var tenimg;
            if(document.getElementById("ImagePath").value == "")
            {
                tenimg = document.getElementById("hidImg").value;
                capnhathinh = 0;
            }
            else
            {
                tenimg = document.getElementById("ImagePath").files[0].name; 
            }
            var trang = <%= ViewData["page"]%>
            
            var json = {
                updImg:capnhathinh,
                page:trang,
                pathHinh: pathimg,

                MaSP:masp,
                TenSP: tensp,
                HinhAnh:tenimg,
                Gia:g,
                SoLuongDatToiThieu:sltt,
                KhaNangCungUng:kncu,
                LoaiThanhToan:ltt,
                MaDanhMuc:mdm,
                MaQuocGia:mqg,
                MaTV:mtv,
                MaDVT:mdvt,
                TinhTrang:mtt,
                ChiTiet:ct
            };
            $.ajax({
                url: '/Admin/AdminManageProduct',
                type: 'POST',
                contentType: 'application/json',
                dataType: 'json',
                data: JSON.stringify(json),
                success: function (response) {
                    alert(response);
                }
            });
            alert("Update success");
        }

    </script>
    <h4>Management Product</h4>
    <table cellpadding="5" cellspacing="5" border="1" style="width: 100%">
        <tr>
            <th>Name product</th>
            <th>Price</th>   
            <th>Supply Capacity</th>   
            <th>Catalog</th>
            <th>Member</th>
            <th>Status</th>
        </tr>
    <% for (int i = 0; i < ((ArrayList)ViewData["DanhSachSanPham"]).Count; i++) 
       {
           SanPham sp = ((SanPham)((ArrayList)ViewData["DanhSachSanPham"])[i]);%>
        <tr>
            <td style="width:400px"><%=sp.TenSP %></td>                                   
            <td><%=sp.Gia %></td>
            <td><%=sp.KhaNangCungUng %></td>
            <td><%=sp.TenDanhMuc %></td>
            <td><%=sp.TenTV %></td>
            <td>
                    <% switch (sp.TinhTrang)
                        { 
                            case 0: %>
                                <h6>Waiting Accept</h6>
                                <%break;
                            case 1:%>
                                <h6>Reject</h6>
                                <%break;
                            case 2:%>
                                <h6>Accept</h6>
                                <%break;
                            default:%>
                                <%break;
                        } %>                               
            </td>
            <td><a href="AdminUpdateProduct?id=<%=sp.MaSP %>&tinhtrang=-1">Update</a></td>
        </tr>
    <% } %>
        <tr>
            <td colspan="7">
                <% 
                    int totalRow = (int)ViewData["TongSanPham"];
                    int totalPage = 0;
                    int itemInPage = 10;
                    if (totalRow % itemInPage == 0)
                    {
                        totalPage = totalRow / itemInPage;
                    }
                    else
                    {
                        totalPage = (totalRow / itemInPage) + 1; 
                    }
                    for (int i = 1; i <= totalPage; i++)
                    { 
                        int cur_page = (int)ViewData["page"];
                        if(i == cur_page)
                        { %> 
                            <a href="/Admin/AdminManageProduct?id=<%= i %>">[<%= i %>]</a>
                     <% } 
                        else 
                        { %>
                            <a href="/Admin/AdminManageProduct?id=<%= i %>"><%= i %></a>
                     <% }
                    } 
                    %>
            </td>
        </tr>
    </table>
</asp:Content>
